package com.ciwong.epaper.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.ShareBean;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.share.QQShareHelper;
import com.ciwong.epaper.modules.share.QQShareListener;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.modules.share.WxShareListener;
import com.ciwong.epaper.modules.share.bean.ShareMsgObj;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.FileUtil;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.utils.toast.ToastCustomView;
import com.tencent.tauth.UiError;

/**
 * 分享的dialog
 * <p>
 * Created by lzr on 2017/6/2.
 */
public class ShareDialog extends Dialog implements View.OnClickListener {

    private View mWeiXinFr, mWeiXin
            , mQQFr ,mQQSpace;

    private ShareBean shareBean;

    private int shareBitmapId;

    private String shareQQIocn;

    private String shareContent;

    private Activity mActivity;

    private ImageView share_qq;
    private ImageView share_qq_space;
    private String doWorkId;
    private String mAddPointType;

    public ShareDialog(Context context, ShareBean shareBean) {
        super(context, R.style.read_choose_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.share_dialog, null);
        setContentView(view);
        this.shareBean = shareBean;
        mActivity = (Activity) context;
        initView();
        initEve();
        setCanceledOnTouchOutside(true);
        bindData();
        isSupportQQAndQQZone();

        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        layoutParams.height = (int) context.getResources().getDimension(R.dimen.share_dialog_hei);
        view.setLayoutParams(layoutParams);
        this.getWindow().setGravity(Gravity.BOTTOM);
    }


    private void bindData() {
        if (shareBean != null) {
            // moduleId =-1排行榜/ 5(单元测试)/7(时文)/10(同步跟读)/15(听说模考)/30(报听写)/126(视频讲解)/124(线上作答)/1pk SHARE
            switch (shareBean.getModuleId()) {
                case 10:
                case 30:
                case 127:
                    shareBitmapId = R.drawable.word_share_icon;
                    shareQQIocn = FileUtil.getShareWordIconLocalPath(getContext());
                    String score = getContext().getString(R.string.speech_word_score, shareBean.getScore());
                    shareContent = getContext().getString(R.string.str_share_contentword, score);
                    break;
                case 124:
                    shareBitmapId = R.mipmap.onlineanswer_share_icon;
                    shareQQIocn = FileUtil.getShareOnlineAnswerIconLocalPath(getContext());
                    String onlineAnswer_score = getContext().getString(R.string.speech_word_score, shareBean.getScore());
                    shareContent = getContext().getString(R.string.str_share_content, onlineAnswer_score);
                    if (shareBean.getWorkStatus() == 17) {
                        shareContent = getContext().getString(R.string.str_share_content2);
                    }
                    break;
                case -1:
                    shareBitmapId = R.mipmap.rank_share_icon;
                    shareQQIocn = FileUtil.getShareRankIconLocalPath(getContext());
                    shareContent = "快来看看我在作业排行榜上的排名吧~";

                    break;
                case 1:
                    shareBitmapId = R.mipmap.pk_share_icon;
                    shareQQIocn = FileUtil.getSharePKIconLocalPath(getContext());
                    shareContent = "习习向上单词PK,来一场真学霸之间的战斗吧.";

                    break;
                default:
                    shareBitmapId = R.drawable.lsw_share_icon;
                    shareQQIocn = FileUtil.getShareIconLocalPath(getContext());
                    String tempScore = getContext().getString(R.string.speech_word_score, shareBean.getScore());
                    if (shareBean.getWorkStatus() != 15 && shareBean.getWorkStatus() != 16 && shareBean.getWorkStatus() != 17) {
                        shareContent = getContext().getString(R.string.str_share_content, tempScore);
                    } else {
                        shareContent = getContext().getString(R.string.str_share_content_noscore);
                    }
                    break;
            }
        }
    }


    /**
     * 初始化控件
     */
    private void initView() {
        mWeiXinFr = findViewById(R.id.weixin_friend_lin);
        mWeiXin = findViewById(R.id.weixin_lin);
        mQQFr=findViewById(R.id.qq_share_lin);
        mQQSpace=findViewById(R.id.space_lin);
        share_qq = (ImageView) findViewById(R.id.share_qq);
        share_qq_space = (ImageView) findViewById(R.id.share_qq_space);

    }


    public void setmQQFrGone(boolean isGone){
        if(isGone){
            mQQFr.setVisibility(View.GONE);
            mQQSpace.setVisibility(View.GONE);
        }else {
            mQQFr.setVisibility(View.VISIBLE);
            mQQSpace.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 初始化事件
     */
    private void initEve() {
        mWeiXinFr.setOnClickListener(this);
        mWeiXin.setOnClickListener(this);
        share_qq.setOnClickListener(this);
        share_qq_space.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.weixin_lin) {
            weixinShare(1);
        } else if (v.getId() == R.id.weixin_friend_lin) {
            weixinShare(2);
        } else if (v.getId() == R.id.share_qq) {
            qqShare();
        } else if (v.getId() == R.id.share_qq_space) {
            qqZoneShare();
        }
        //dismiss();
    }

    /**
     * @param type 1等于微信 2 等于朋友圈
     */
    private void weixinShare(int type) {
        try {
            WxShareHelper.getInstance().initWxShare(mActivity, new WxShareListener() {
                @Override
                public void onWxShareing(String str) {
                    CWLog.d("Share==>> ", "ShareDialog onWxShareing：" + str);
                    System.out.println("Share==>> " + "ShareDialog onWxShareing：" + str);
                }

                @Override
                public void onWxShareSucc(String var) {
                    CWLog.d("Share==>> ", "ShareDialog onWxShareSucc成功：" + var);
                    System.out.println("Share==>> " + "ShareDialog onWxShareSucc成功：" + var);
                    boolean flag = shareAddPointType(doWorkId, mAddPointType);
                    if (!flag){//当未调用加积分接口时
                        ToastCustomView.getInstance().toastShowCenterSuccess(EApplication.getInstance().getResources().getString(R.string.share_success), Toast.LENGTH_SHORT);
                    }
                }

                @Override
                public void onWxShareFailure(int var, String var1) {
                    System.out.println("Share==>> " + "ShareDialog onWxShareFailure：" + var);
                    CWLog.d("Share==>> ", "ShareDialog onWxShareFailure：" + var);
                }

                @Override
                public boolean onWxShareCancel() {
                    System.out.println("Share==>> " + "ShareDialog onWxShareCancel：");
                    CWLog.d("Share==>> ", "ShareDialog onWxShareCancel：");
                    return false;
                }
            });
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setWxType(type);
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            Bitmap bmp = BitmapFactory.decodeResource(this.getContext().getResources(), shareBitmapId);
            shareMsgObj.setThunbBmp(bmp);
            shareMsgObj.setUrl(shareBean.getUrl());
            switch (type) {
                case 1:
                    shareMsgObj.setMsgTitle(shareBean.getTitle());
                    shareMsgObj.setMsgDesription(shareContent);
                    break;
                case 2:
                    shareMsgObj.setMsgTitle(shareContent);
                    break;
            }
            WxShareHelper.getInstance().sendReq(shareMsgObj);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * qq空间分享
     */
    private void qqZoneShare() {
        try {
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            shareMsgObj.setImg_url(shareQQIocn);
            shareMsgObj.setUrl(shareBean.getUrl());
            shareMsgObj.setMsgTitle(shareContent);
            QQShareHelper.getInstance().shareToQZone(mActivity, shareMsgObj, new QQShareListener() {

                @Override
                public void onQQShareSucc(Object var) {
                    System.out.println("Share==>> " + "onQQShareSucc：" + var);
                    CWLog.d("Share==>> ", "onQQShareSucc：" + var);
                    boolean flag = shareAddPointType(doWorkId, mAddPointType);
                    if (!flag){//当未调用加积分接口时
                        ToastCustomView.getInstance().toastShowCenterSuccess(EApplication.getInstance().getResources().getString(R.string.share_success), Toast.LENGTH_SHORT);
                    }
                }

                @Override
                public void onQQShareFailure(UiError var1) {
                    System.out.println("Share==>> " + "onQQShareFailure：" + var1);
                    CWLog.d("Share==>> ", "onQQShareFailure：" + var1);

                }

                @Override
                public boolean onQQShareCancel() {
                    System.out.println("Share==>> " + "onQQShareCancel：");
                    CWLog.d("Share==>> ", "onQQShareCancel：");
                    return false;
                }
            });
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    /**
     * QQ分享
     */
    private void qqShare() {
        try {
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            shareMsgObj.setMsgTitle(shareBean.getTitle());
            shareMsgObj.setLocalImgUrl(shareQQIocn);
            shareMsgObj.setUrl(shareBean.getUrl());
            shareMsgObj.setMsgDesription(shareContent);
            QQShareHelper.getInstance().shareToQQ(mActivity, shareMsgObj, new QQShareListener() {
                @Override
                public void onQQShareSucc(Object var) {
                    boolean flag = shareAddPointType(doWorkId, mAddPointType);
                    if (!flag){//当未调用加积分接口时
                        ToastCustomView.getInstance().toastShowCenterSuccess(EApplication.getInstance().getResources().getString(R.string.share_success), Toast.LENGTH_SHORT);
                    }
                }

                @Override
                public void onQQShareFailure(UiError var1) {
                }

                @Override
                public boolean onQQShareCancel() {
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void isSupportQQAndQQZone() {
        if (EConstants.QQ_APP_ID.equals("") || null == EConstants.QQ_APP_ID) {
            share_qq.setImageResource(R.mipmap.share_qq_unstalled);
            share_qq_space.setImageResource(R.mipmap.share_qq_space_unstalled);
            share_qq.setEnabled(false);
            share_qq_space.setEnabled(false);
        }
    }

    public ShareDialog setParamForAddPoint(String doWorkId, String type) {
        this.doWorkId = doWorkId;
        this.mAddPointType = type;
        return this;
    }

    /**
     * 是否调用了添加积分接口 true 调用了 false 未调用
     * @param doWorkId
     * @param type
     * @return
     */
    private boolean shareAddPointType(String doWorkId, String type) {
        if (doWorkId==null||type==null)return false;
        MeDao.getInstance().addPoint(EApplication.getInstance().getUserInfoBase().getUserId() + "", EApplication.getInstance().getUserInfoBase().getRealName(), type, doWorkId + "");
   return true;
    }

}
